let _ = require('./tools/lodash.js');
let content=require("./testData").content


let words=content.toLowerCase().match(/\w+/g);

let tuples=words.map(function (word) {
    return [word,1]
});
let wordsCountResult=reduceByKey(tuples,(left,right)=>left+right)

function reduceByKey(tuples,reduceCallback) {
    let grouped=_.groupBy(tuples,tuple=>tuple[0]);
    console.log("g",grouped)
    return _.toPairs(_.mapValues(grouped,tuples=>_.chain(tuples).map(tuple=>tuple[1]).reduce(reduceCallback).value()))
}

let sortByDesc=function(arr,index){
    return arr.sort((b,a)=>a[index]-b[index]);
};
let sortByAsc=function(arr,index){
    return arr.sort((a,b)=>a[index]-b[index]);
}


 let sorted= sortByDesc(wordsCountResult,1)

let top5=_.take(sorted,5).map(tuple=>tuple[0]);
let minimal5=_.takeRight(sorted,5).map(tuple=>tuple[0])
console.log(top5,minimal5)